<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>QuickSort.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

	<script type="text/javascript">
		var a = new Array(9,10,5,7,33,6,22,77,4,1);
		function quickSort(l,r){
			var lo = l;//左边位置
			var ro = r;//右边位置
			var mid;//中间数
			if(lo < ro){//选出一个中间数
				var midPos = Math.floor((lo+ro)/2);
				mid = a[midPos];
			}
			while(lo < ro){
				//从左边往右边找，当遇到比中间数大的就停下来等待交换
				while((lo < r) && (a[lo] < mid))lo++;
				//从右边往左边找，当遇到比中间数小的就停下来等待交换
				while((ro > l) && (a[ro] > mid))ro--;
				if(lo <= ro){//进行交换
					swap(lo,ro);
					lo++;
					ro--;
				}
			}
			//alert(a[lo]+"-"+a[ro]);
			//alert(a+" mid:"+mid+" lo:"+(lo+1)+" ro:"+(ro+1));
			if(l < ro)
					quickSort(l,ro);
			if(lo < r)
					quickSort(lo,r);
			
		}
		
		function swap(i, j){
	      var T;
	      T = a[i];
	      a[i] = a[j];
	      a[j] = T;
	
	   }
		
	</script>
  </head>
  
  <body onload="quickSort(0,a.length-1)">
    <input type="button" value="显示排序结果" onclick="alert(a);">
  </body>
</html>
